package com.fxkj.cloud.drms.ui.mqtt.handler.receive;

import com.fxkj.cloud.drms.application.AlarmHistoryApplicationService;
import com.fxkj.cloud.drms.infra.mq.mqtt.MqttConstants;
import com.fxkj.cloud.drms.ui.mqtt.handler.AbstractReceiveHandler;
import com.fxkj.cloud.drms.ui.mqtt.model.AlarmMsg;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/7 16:06
 */
@Component(MqttConstants.Topic.Receive.DEVICE_ALARM_CREATE_SERVICE_NAME)
public class DeviceAlarmCreateReceiverHandler extends AbstractReceiveHandler<AlarmMsg> {
    private final Logger logger = org.slf4j.LoggerFactory.getLogger(DeviceAlarmCreateReceiverHandler.class);
    private final AlarmHistoryApplicationService alarmHistoryApplicationService;

    public DeviceAlarmCreateReceiverHandler(AlarmHistoryApplicationService alarmHistoryApplicationService) {
        this.alarmHistoryApplicationService = alarmHistoryApplicationService;
    }

    @Override
    protected void doHandler(String topic, AlarmMsg data, String... topicParams) throws Exception {
        logger.trace("接受到设备告警消息：{}", data);
        String deviceSerialNo = topicParams[0];
        alarmHistoryApplicationService.create(deviceSerialNo, data.getTs(), data.getAlarmCode(), data.getAlarmText(), data.getAlarmValue());
    }

    @Override
    protected Class<AlarmMsg> getClazz() {
        return AlarmMsg.class;
    }
}
